/**
 * 详情页面的js
 */

//判断密码是否合格：
//小于 0 不合格 －1 小于六位|  －2 大于20位  －3 连续数字、字母|  －4 同一个数字／字母
function validatePasswordStrength(pass) {
    var r = 0;
    if (pass.length < 6) {
        r = -1;
    } else if (pass.length > 20) {
        r = -2;
    } else {
        r = isRepeatStr(pass);
    }
    return r;
}
//判断是否是重复的字符
function isRepeatStr(str) {
    arr = str.split("");
    count_ = arr.length;
    var flag = false;//是否合格
    var t = 0;
    for (i = 0; i < (count_ - 1); i++) {
        if (arr[i] != arr[i + 1]) {
            flag = true;
            break;
        }
    }
    if (flag) {
        var flag1 = false;
        var flag2 = false;
        if (str.match(/^[0-9]+$/) || str.match(/^[a-z]+$/) || str.match(/^[A-Z]+$/)) {
            a_ = [];
            for (var i = 0; i < count_; i++) {
                a_.push(arr[i].charCodeAt(0));
            }
            for (var i = 0; i < count_ - 1; i++) {
                if ((a_[i] - a_[i + 1]) != 1) {
                    flag1 = true;
                    break;
                }
            }
            for (var i = 0; i < count_ - 1; i++) {
                if ((a_[i + 1] - a_[i]) != 1) {
                    flag2 = true;
                    break;
                }
            }
            flag = flag1 && flag2;
        } else {
            flag = true;
        }
        if (!flag) {
            t = -3;
        }
    } else {
        t = -4;
    }
    return t;
}

//0 email, 1 name, 3 pwd, 4pwd-repeat,
var flags = [false,false,false,false,false];

function enableSubmit(bool){
    if (bool) {
        $("#input-submit").removeAttr("disabled");
        $("#input-submit").addClass('input-submit');
        $("#input-submit").removeClass('submit-disabled');
    } else {
        $("#input-submit").attr("disabled","disabled");
        $("#input-submit").addClass('submit-disabled');
        $("#input-submit").removeClass('input-submit');
        
    }
}

function submitButton() {
    for (f in flags) if(!flags[f]) {
        enableSubmit(false); 
        return false;
    }
    enableSubmit(true);
}

function checkEmail() {
    var email = $('#email').val();
    if (email=='' || email==null) {
        $('#email').next('span').text('请填写邮箱地址！');
        flags[0] = false;
        submitButton();
        return false;
    } 
    var reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;
    if(!reg.test(email)) {
        $('#email').next('span').text('请填写正确的邮箱格式！');
        flags[0] = false;
        submitButton();
        return false;
    }
    flags[0] = true;
    submitButton();
    $('#email').next('span').text('');
    return true;
}
function checkUserName() {
    var name = $('#username').val();
    if (name=='' || name==null) {
        $('#username').next('span').text('请填写用户名！');
        flags[1] = false;
        submitButton();
        return false;
    }
    if(name.length>30 || name.length<3) {
        $('#username').next('span').text('用户名3-30个字符！');
        flags[1]=false;
        submitButton();
        return false;
    }
    flags[1] = true;
    submitButton();
    $('#username').next('span').text('');
    return true;
}
//小于 0 不合格 －1 小于六位|  －2 大于20位  －3 连续数字、字母|  －4 同一个数字／字母
function checkPwd() {
    var pass = $('#password').val();
    if (pass=='' || pass==null) {
        $('#password').next('span').text('请填写密码！');
        flags[2] = false;
        submitButton();
        return false;
    }
    var l = validatePasswordStrength(pass);
    if (l < 0) {
        if (l==-1 || l== -2) {
            $('#password').next('span').text('密码长度6-20！');
        }
        if (l==-3) {
             $('#password').next('span').text('连续的字母或者数字！');
        }
        if (l==-4) {
             $('#password').next('span').text('相同的字母或者数字！');
        };
        flags[2] = false;
        submitButton();
        return false;
    }
    flags[2] = true;
    submitButton();
    $('#password').next('span').text('');
    return true;
}
function checkPwdRepeat() {
    var pass = $('#password').val();
    var passRepeat = $('#password_repeat').val();
    if (passRepeat=='' || passRepeat==null) {
        $('#password_repeat').next('span').text('请填写重复密码！');
        flags[3] = false;
        submitButton();
        return false;
    }
    if (pass != passRepeat) {
        $('#password_repeat').next('span').text('两次密码填写不一致！');
        flags[3] = false;
        submitButton();
        return false;
    }
    flags[3] = true;
    submitButton();
    $('#password_repeat').next('span').text('');
    return true;
}
function checkCaptcha() {
    var captcha = $('#captcha').val();
    if (captcha=='' || captcha==null) {
        $('.captcha-a').next('span').text('请填写验证码！');
        flags[4] = false;
        submitButton();
        return false;
    }
    flags[4] = true;
    submitButton();
    $('.captcha-a').next('span').text('');
    return true;
}
$(function() {
    $('#userRegForm').on('submit', function(){
        checkUserName();
        checkEmail();
        checkPwd();
        checkPwdRepeat();
        //checkCaptcha();
        for (f in flags) if(!flags[f]) {
            enableSubmit(false); 
            return false;
        }
        return true; 
    });
    $('.captcha-a').on('click', function(){
        $('#captcha-img').attr('src', '/user/captcha?t='+Math.random());
       // $(this).before(img);
    });
});
